package com.bcbim.common.util;

import com.bcbim.common.sys.Constant;
import com.bcbim.common.sys.ParamException;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Map;

/**
 * @Description: 参数校验
 * @Author: ye
 * @Date: 2020/9/30
 * @Version: V1.0
 **/
public class ParamCheck {
    /**
     * @Description: 参数校验
     * @Param: id
     * @Author: ye
     * @Date: 2020/9/30
     */
    public static void idCheck(String id) throws ParamException {
        if (null == id || id.length() != Constant.ID_LENGTH) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        }
    }

    /**
     * 字符串能够为null，不为null的时候满足正则表达式的校验
     *
     * @param str    需要校验的字符
     * @param regExp 正则表达式
     * @author wxc
     * @date 2021/8/13  15:26
     */
    public static void stringCanEmptyRegexpCheck(String str, String regExp) throws ParamException {
        if (StringUtils.isNotEmpty(str) && !str.matches(regExp)) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        }
    }

    /**
     * @Description: 页码参数校验
     * @Param: pageNum, pageSize
     * @Author: ye
     * @Date: 2020/9/30
     */
    public static void pageNumCheck(Integer pageNum, Integer pageSize) throws ParamException {
        if (pageNum == null || pageSize == null || pageNum == 0 || pageSize == 0) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        }
    }

    /**
     * @Description: 字符串校验 不可为空
     * @Param: String
     * @Param: Integer 长度
     * @Return:
     * @Author: ye
     * @Date: 2021/6/21
     */
    public static void stringNotEmpty(String s, Integer i) throws ParamException {
        if (ObjectUtils.isEmpty(s) || s.length() > i || s.length() == 0) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        }
    }

    /**
     * @Description: 字符串长度校验
     * @Param: length s
     * @Author: ye
     * @Date: 2021/1/5
     */
    public static void stringNotNullLengthCheck(Integer length, String s) throws ParamException {
        if (null == length || null == s || s.length() > length || s.length() == 0) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        }
    }

    public static void stringCanBeNullLengthCheck(Integer length, String s) throws ParamException {
        if (null != s && s.length() > length) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        }
    }

    /**
     * @Description: 名字校验
     * @Param: name
     * @Author: ye
     * @Date: 2020/10/22
     */
    public static void nameCheck(String name) throws ParamException {
        if (name == null || name.length() > 18) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        }
    }

    /**
     * @Description: flag校验 (限0或1)
     * @Param: flag
     * @Author: ye
     * @Date: 2020/12/3
     */
    public static void flagCheck(String flag) throws ParamException {
        if (!flag.equals("0") && !flag.equals("1")) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        }
    }

    /**
     * @Description: 时间校验
     * @Param: LocalDateTime
     * @Author: ye
     * @Date: 2020/11/13
     */
    public static void dateTimeCheck(LocalDateTime localDateTime) throws ParamException {
        if (localDateTime == null) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        }
    }

    /**
     * @Description: 字符串校验 可为空
     * @Param: String
     * @Param: Integer 长度
     * @Return:
     * @Author: ye
     * @Date: 2021/6/21
     */
    public static void stringCanBeEmpty(String s, Integer i) throws ParamException {
        if (ObjectUtils.isNotEmpty(s) && s.length() > i) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        }
    }

    /**
     * @Description: 可为空的id校验
     * @Param: id
     * @Author: haoshuai
     * @Date: 2021/7/6
     */
    public static void idCanBeEmpty(String id) throws ParamException {
        if (StringUtils.isNotEmpty(id) && id.length() != Constant.ID_LENGTH) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        }
    }

    /**
     * @Description: 对象不为空校验
     * @Param: Object
     * @Return:
     * @Author: ye
     * @Date: 2021/6/21
     */
    public static void objectNotEmpty(Object o) throws ParamException {
        if (ObjectUtils.isEmpty(o)) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        }
    }

    /**
     * 字符串不为null 并且满足正则表达式
     *
     * @param str    需要校验的字符
     * @param regExp 正则表达式
     * @author wxc
     * @date 2021/8/13  15:21
     */
    public static void stringNotEmptyRegexpCheck(String str, String regExp) throws ParamException {
        if (StringUtils.isEmpty(str) || !str.matches(regExp)) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        }
    }

    /**
     * @Description: 字符串校验 不可为空
     * @Param: String
     * @Param: Integer 长度
     * @Author: ye
     * @Date: 2021/6/21
     */
    public static void notNullString(String s, Integer length) throws ParamException {
        if (ObjectUtils.isEmpty(s) || s.length() > length || s.length() == 0) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        }
    }

    /**
     * @Description: 字符串校验 可为空
     * @Param: String
     * @Param: Integer 长度
     * @Author: ye
     * @Date: 2021/6/21
     */
    public static void nullableString(String s, Integer length) throws ParamException {
        if (null != s && s.length() > length) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        }
    }

    /**
     * @Description: 校验对象不为空
     * @Param: Object
     * @Return:
     * @Author: ye
     * @Date: 2022/3/29
     */
    public static void notNull(Object object) throws ParamException {
        if (object == null) {
            throw new ParamException(ParamException.PARAMETER_ERROR);
        } else if (object instanceof CharSequence) {
            if (((CharSequence) object).length() == 0) {
                throw new ParamException(ParamException.PARAMETER_ERROR);
            }
        } else if (object.getClass().isArray()) {
            if (Array.getLength(object) == 0) {
                throw new ParamException(ParamException.PARAMETER_ERROR);
            }
        } else if (object instanceof Collection) {
            if (((Collection) object).isEmpty()) {
                throw new ParamException(ParamException.PARAMETER_ERROR);
            }
        } else if (object instanceof Map) {
            if (((Map) object).isEmpty()) {
                throw new ParamException(ParamException.PARAMETER_ERROR);
            }
        }
    }
}